<?php
namespace Yzadmin\Controller;
use Think\Controller;
/**
* 会员管理
*/
class UserController extends CkController{
 

   //会员列表
   public function index()
   {  
      $get=I('get.');
      $M=M("user");
      $where='';
      if($get['select'] && $get['nr']){
        if($get['select']=="login_ip"){
           $get['nr']=iplong($get['nr']);
        }
        $where = $get['select']."=".$get['nr'];
        if($get['select']=='nickname'){
           $where=array('nickname'=>array('like','%'.$get['nr'].'%'));
        }
        $M->where($where);
      }

      $order= "id desc";
      if($get['order']){
        $order= $get['order'] ." desc";
      }
      $count = $M->count(1);// 查询满足要求的总记录数
      $Page  = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
      $Page->setConfig('prev', "上一页");//上一页
      $Page->setConfig('next', '下一页');//下一页
      $Page->setConfig('first', '首页');//第一页
      $Page->setConfig('last', "末页");//最后一页
      $Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
      
      $data['user']=$M->where($where)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();

      $data['page'] = $Page->show();// 分页显示输出
      $data['title']="会员列表";
      $this->assign($data);
      $this->display();
   }
   

   public function qingli()
   {
     $data=I('get.');
     if(empty($data['balance']) && empty($data['login_time'])){
        $this->error("账号余额,和最低登陆时间不可以为空.");
     }
     $data['login_time']= (int)$data['login_time'];
     $login_time= time() - ((3600*24)*$data['login_time']);
     $where=array(
        'balance'=>array("LT",$data['balance']),
        'login_time'=>array("LT",$login_time),
        );
     $this->log_qingli($where);
    if(M("user")->where($where)->delete()!==false){
        $this->success("清理成功");
     }else{
         $this->success("清理失败.");
     }
   }


   //关联日志清理
   private  function log_qingli($where)
   { 
      $users=M('user')->field('id')->where($where)->select();
      if(empty($users))
      {
        return false;
      }
      $array=array();
      foreach ($users as $v) {
        $array[]=$v['id'];
      }
     M('tixian')->where(array('uid'=>array("in",$array)))->delete();
     M("jiakuan_log")->where(array('uid'=>array("in",$array)))->delete();
     M("jiakuan_xiaji_log")->where(array('uid'=>array("in",$array)))->delete();
     M("jifeiip_log")->where(array('uid'=>array("in",$array)))->delete();
     M("kouliangip_log")->where(array('uid'=>array("in",$array)))->delete();
     M("lanjieip_log")->where(array('uid'=>array("in",$array)))->delete();
   }
   
   
   //删除
   public function del()
   {
     $id=(int)I('get.id');
     $where=array('uid'=>$id);
     $data=M("user")->where(array('id'=>$id))->delete();
     M('tixian')->where($where)->delete();
     M("jiakuan_log")->where($where)->delete();
     M("jiakuan_xiaji_log")->where($where)->delete();
     M("jifeiip_log")->where($where)->delete();
     M("kouliangip_log")->where($where)->delete();
     M("lanjieip_log")->where($where)->delete();     
     if($data!==false)
     {
        $this->success("删除成功!");
     }else
     {  
        $error=mysql_error();
        $this->error("删除失败!错误状态码:".$error);
     }
   }
   
   //修改
   public function update()
   {
     $id=(int)I('get.id');
     $data['user']=M('user')->find($id);
     //echo long2ip($data['user']['login_ip']); die();
     $data['title']="会员修改";
     $this->assign($data);
     $this->display();
   }
   

   //修改处理
   public function updates()
   {
     $data=I("post.");
     if(empty($data['password'])){
        unset($data['password']);
     }else{
        $data['password']=md5($data['password']);
     }
    if(M('user')->where('id='.$data['id'])->save($data)!==false){
       $this->success("会员更新成功!",U("user/index"));
    }else{
    $this->error("会员更新失败错误码:".mysql_error());
    }
 
   }

  
 


}